/* 
> --------------------------------------------------------------------------------- 
> -- Copyright (C) THALES AIR SYSTEMS SA 2005-2009 all rights reserved. 
> -- This software is the property of THALES AIR SYSTEMS SA and may not be used in 
> -- any manner except under a licence agreement signed with THALES AIR SYSTEMS SA. 
> --------------------------------------------------------------------------------- 
*/ 
package utils;

import java.lang.reflect.Field;


public class Constant {
    
	final public static int DATE_LENGHT =20;
	final public static long DELETE_EVENT = 2; 
	final public static int HANDLE_SIZE = 8; 
	final public static int HEADER_SIZE_UPDATES = 56; // SUT [20] + OT [20] + event [8] + handle [8]
	final public static int HEADER_SIZE_SNAPSHOTS = 48; // SUT [20] + OT [20] + event [8] + handle [8]
	
	final public static String PATH_SO_UPDATES = "/home/pdambrosio/REC/disk1/REC_directories/Session_0001/Updates/Updates-2010.10.07-13.02.12";
	
	private static Field getFirstAttribute(Class<?> c) {
		Field firstField = c.getFields()[0];
		String firstType = firstField.getType().toString();
		if (firstType.equals("boolean") || 
				firstType.equals("byte") ||
				firstType.equals("char") ||
				firstType.equals("short") ||
				firstType.equals("int") ||
				firstType.equals("long") ||
				firstType.equals("float") ||
				firstType.equals("double") ||
				firstType.equals("class java.lang.String")) {
			return firstField;
		}
		else {
			return getFirstAttribute(firstField.getType());
		}
	}	
	
	
	/** Calculates the skip needed by a the marshalled data of a DDS recording.
	 * Once the header is read, a long (the handle) and a byte (endianess) must be read. The cursor is then at position 9.
	 * To perform a good reading of the marshalled data that come next, we need to skip some bytes, depending on the type of the first attribute of the DDS.
	 * More details in the code.  
	 * @param dds the DDS 
	 * @return the number of bytes that must be skipped
	 */
	
	public static int calculateSkip(String ddsType) {
		int res = 0;
		try {
			Class<?> myClass = Class.forName(ddsType);
			Field firstAttribute = getFirstAttribute(myClass);
			
			String firstType = firstAttribute.getType().toString();
			
			// These types are 1 byte size. We need an byte alignment of 1. 
			// 9 mod 1 = 0, so we can skip no byte.
			if (firstType.equals("boolean") ||
					firstType.equals("byte") ||
					firstType.equals("char")) {
				res = 0;
			}
			// This type is 2 bytes size. We need an byte alignment of 2. 
			// 9 mod 2 != 0, so we must skip 1 byte to reach position 10.
			else if (firstType.equals("short")) {
				res = 1;
			}
			// These types are 4 bytes size. We need an byte alignment of 4. 
			// 9 mod 4 != 1, so we must skip 3 byte to reach position 12.
			else if (firstType.equals("int") || 
					firstType.equals("float") ||
					firstType.equals("class java.lang.String")) {
				res = 3;
			}
			// These types are 8 bytes size. We need an byte alignment of 8. 
			// 9 mod 8 != 1, so we must skip 7 byte to reach position 16.
			else if (firstType.equals("long") || 
					firstType.equals("double")) {
				res = 7;
			}
			else {
				throw new Exception("ERROR : DDS not handled by the application");
			}
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
}
